package com.lwphk.easydocs.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpCookie;

import com.lwphk.easydocs.core.vo.DocsBasicInfo;

/**
 * 全局映射字段加载器
 * @author liwenping
 *
 */
public class GlobalConfigLoadHandler {
	
	private static final Logger log = LoggerFactory.getLogger(GlobalConfigLoadHandler.class);

	private static final String FIELD_MAPPING_FILE = "easy-docs.txt";
	private static final String FIELD_MAPPING_FILE2 = "easydocs.txt";
	
	public GlobalConfigLoadHandler() {
		DocsBasicInfo.ExcludeParamSet.add("javax.servlet.ServletRequest");
		DocsBasicInfo.ExcludeParamSet.add("javax.servlet.ServletResponse");
		DocsBasicInfo.ExcludeParamSet.add("javax.servlet.HttpSession");
		DocsBasicInfo.ExcludeParamSet.add(HttpCookie.class.getName());
		
		DocsBasicInfo.ExcludeParamSet.add("javax.servlet.http.HttpServletRequest");
		DocsBasicInfo.ExcludeParamSet.add("javax.servlet.http.HttpServletResponse");
		DocsBasicInfo.ExcludeParamSet.add("org.springframework.ui.Model");
		DocsBasicInfo.ExcludeParamSet.add("org.springframework.web.servlet.ModelAndView");
		DocsBasicInfo.ExcludeParamSet.add("org.springframework.ui.ModelMap");
		
		loadEasyDocsConfig();
	}
	
	public void loadEasyDocsConfig() {
	
		ClassPathResource resource = new ClassPathResource(FIELD_MAPPING_FILE);
		if(!resource.exists()) {
			resource = new ClassPathResource(FIELD_MAPPING_FILE2);
		}
		
		if(!resource.exists()) {
			return;
		}
		
		Map<String, String> fieldsMappingMap = DocsBasicInfo.GlobalFieldMappingMap;
		
		Set<String> excludeParamSet = DocsBasicInfo.ExcludeParamSet;
		
		String currentSection = null;
        try (BufferedReader reader = new BufferedReader(new FileReader(resource.getFile()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if(line.startsWith("#")) {
					continue;
				}
                if (line.startsWith("[") && line.endsWith("]")) {
                    currentSection = line.substring(1, line.length() - 1);
                } else if (!line.isEmpty()) {
                    String[] arr = StringUtils.split(line,"=");
                    if (currentSection != null) {
                        switch (currentSection) {
                            case "FieldMapping":
                            	if(arr.length == 2) {
                            		fieldsMappingMap.put(arr[0], arr[1]);
                            	}
                                break;
                            case "ExcludeParam":
                            	excludeParamSet.add(arr[0]);
                                break;
                        }
                    }
                }else {
                	  String[] arr = StringUtils.split(line,"=");
                	  if(arr.length == 2) {
                  		fieldsMappingMap.put(arr[0], arr[1]);
                  	}
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	
	
//	public void loadFieldMapping() {
//		log.info("加载全局字段映射文件 {}",FIELD_MAPPING_FILE);
//		ClassPathResource resource = new ClassPathResource(FIELD_MAPPING_FILE);
//		if(!resource.exists()) {
//			return;
//		}
//		Map<String, String> fieldsMappingMap = DocsBasicInfo.GlobalFieldMappingMap;
//		try {
//			BufferedReader reader = new BufferedReader(new FileReader(resource.getFile()));
//			String line = null;
//			
//			while((line = reader.readLine()) != null) {
//				if(line.startsWith("#")) {
//					continue;
//				}
//				
//				String[] arr = StringUtils.split(line,"=");
//				if(arr.length != 2) {
//					continue;
//				}
//				fieldsMappingMap.put(arr[0], arr[1]);
//				
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
//		
//		
//	}
}
